package ar.activity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.provider.Settings;
import android.text.InputType;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import ar.Location.Internet.NetworkStateReceiver;
import ar.Location.PlaceObject.PlaceCategoryManager;
import ar.activity.R.string;
import ar.globals.Globals;
import ar.sql.RouteDatabaseHandler;
import ar.view.adapter.SettingsAdapter;

public class SettingsActivity extends BaseActivity {

	private ListView lvSettings;
	private SettingsAdapter settingsAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.settings);
		super.onCreate(savedInstanceState);
		tvWhatScreen.setText(res.getString(string.settingScreenName));

		settingsAdapter = new SettingsAdapter(this);
		lvSettings = (ListView) findViewById(R.id.lvSettings);
		lvSettings.setAdapter(settingsAdapter);
		initEventClick();
	}

	public void initEventClick() {
		lvSettings.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {
				switch (position) {
				case 0:// maximum number of searched locations
					showInputMaxNumberLoctionsDialog();
					break;
				case 1:// radius
					showInputRaidusDialog();
					break;
				case 2:// period of updating
					showInputPeriodOfUpdatingDialog();
					break;
				case 3:// networks
					startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
					break;
				case 4:// GPS
					startActivity(new Intent(
							Settings.ACTION_LOCATION_SOURCE_SETTINGS));
					break;
				case 5:// server address
					showInputServerAddress();
					break;
				case 6:// clear cached places
					clearCachedPlaces();
					break;
				case 7:// clear saved routes
					clearSavedRoutes();
					break;
				default:
					break;
				}
			}
		});
	}

	private void clearSavedRoutes() {
		RouteDatabaseHandler myDbHelper = new RouteDatabaseHandler(this);
		try {
			myDbHelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}
		boolean success = myDbHelper.clearAll();
		if (success)
			Toast.makeText(getApplicationContext(), "Successfully",
					Toast.LENGTH_LONG).show();
		else
			Toast.makeText(getApplicationContext(), "Not Successfully",
					Toast.LENGTH_LONG).show();
		myDbHelper.close();
	}

	private void clearCachedPlaces() {

	}

	private void showInputServerAddress() {
		AlertDialog.Builder d = new Builder(this);
		d.setMessage(res.getString(string.settingEnterServer));
		d.setTitle(res.getString(string.settingServerAddress));
		final EditText etServAdress = new EditText(this);
		etServAdress.setText(Globals.serverAddress);
		d.setView(etServAdress);

		d.setPositiveButton(res.getString(string.OK), new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				String addr = etServAdress.getText().toString();
				Toast.makeText(getApplicationContext(), addr,
						Toast.LENGTH_SHORT).show();

				Globals.serverAddress = addr;
			}
		});

		d.setNegativeButton(res.getString(string.Cancel), null);
		d.show();
	}

	private void showInputMaxNumberLoctionsDialog() {
		AlertDialog.Builder d = new Builder(this);
		d.setTitle(res.getString(string.settingMaxNumber));
		d.setMessage(res.getString(string.settingMaxNumberNotValid));
		final EditText etNumber = new EditText(this);
		etNumber.setInputType(InputType.TYPE_CLASS_NUMBER);
		etNumber.setText(String
				.valueOf(Globals.MAX_NUMBER_OF_SEARCHED_LOCATIONS));
		d.setView(etNumber);
		d.setPositiveButton(res.getString(string.OK), new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				int c = Integer.valueOf(etNumber.getText().toString());
				if (c <= 0 || c > 30) {
					Toast.makeText(getApplicationContext(),
							res.getString(string.settingMaxNumberNotValid),
							Toast.LENGTH_LONG).show();
					return;
				}
				Toast.makeText(getApplicationContext(), etNumber.getText(),
						Toast.LENGTH_SHORT).show();
				Globals.MAX_NUMBER_OF_SEARCHED_LOCATIONS = Integer
						.valueOf(etNumber.getText().toString());

				// Load du lieu lai tuong ung voi so ket qua tra ve vua thay doi
				if (Globals.LAT != null && Globals.LNG != null
						&& Globals.LAT.length() > 0 && Globals.LNG.length() > 0) {
					float myLat = Float.parseFloat(Globals.LAT);
					float myLng = Float.parseFloat(Globals.LNG);

					if (NetworkStateReceiver.isNetworkConnected) {
						if (!PlaceCategoryManager
								.isProcessingLoadListCategories()) {
							PlaceCategoryManager.loadListCategories(myLat,
									myLng, Globals.RADIUS);
						}

						if (!PlaceCategoryManager
								.isProcessingLoadListDataSources()) {
							PlaceCategoryManager.loadListDataSources(myLat,
									myLng, Globals.RADIUS);
						}
					}
				}
			}
		});

		d.setNegativeButton(res.getString(string.Cancel), null);
		d.show();

	}

	private void showInputRaidusDialog() {
		AlertDialog.Builder d = new Builder(this);
		d.setTitle(res.getString(string.settingRadius));
		d.setMessage(res.getString(string.settingRadiusNotValid)
				+ String.valueOf(Globals.MAX_RADIUS));
		final EditText etNumber = new EditText(this);
		etNumber.setInputType(InputType.TYPE_CLASS_NUMBER);
		etNumber.setText(String.valueOf(Globals.RADIUS));
		d.setView(etNumber);

		d.setPositiveButton(res.getString(string.OK), new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				int r = Integer.valueOf(etNumber.getText().toString());
				if (r <= 0 || r > Globals.MAX_RADIUS) {
					Toast.makeText(getApplicationContext(),
							res.getString(string.settingRadiusNotValid),
							Toast.LENGTH_LONG).show();
					return;
				}
				Toast.makeText(getApplicationContext(), etNumber.getText(),
						Toast.LENGTH_SHORT).show();
				Globals.RADIUS = r;

				// Load du lieu lai tuong ung voi ban kinh vua thay doi
				if (Globals.LAT != null && Globals.LNG != null
						&& Globals.LAT.length() > 0 && Globals.LNG.length() > 0) {
					float myLat = Float.parseFloat(Globals.LAT);
					float myLng = Float.parseFloat(Globals.LNG);

					if (NetworkStateReceiver.isNetworkConnected) {
						if (!PlaceCategoryManager
								.isProcessingLoadListCategories()) {
							PlaceCategoryManager.loadListCategories(myLat,
									myLng, Globals.RADIUS);
						}

						if (!PlaceCategoryManager
								.isProcessingLoadListDataSources()) {
							PlaceCategoryManager.loadListDataSources(myLat,
									myLng, Globals.RADIUS);
						}
					}
				}
			}
		});

		d.setNegativeButton(res.getString(string.Cancel), null);
		d.show();
	}

	private void showInputPeriodOfUpdatingDialog() {
		AlertDialog.Builder d = new Builder(this);
		d.setTitle(res.getString(string.settingPeriod));
		d.setMessage(res.getString(string.settingPeriodNotValid));
		final EditText etNumber = new EditText(this);
		etNumber.setInputType(InputType.TYPE_CLASS_NUMBER
				| InputType.TYPE_NUMBER_FLAG_DECIMAL);
		etNumber.setText(String.valueOf(Globals.PERIOD_OF_UPDATING));
		d.setView(etNumber);

		d.setPositiveButton(res.getString(string.OK), new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				float f = Float.valueOf(etNumber.getText().toString());
				if (f <= 0 || f > 20000) {
					Toast.makeText(getApplicationContext(),
							res.getString(string.settingPeriodNotValid),
							Toast.LENGTH_LONG).show();
					return;
				}
				Toast.makeText(getApplicationContext(), etNumber.getText(),
						Toast.LENGTH_SHORT).show();
				Globals.PERIOD_OF_UPDATING = f;
			}
		});

		d.setNegativeButton(res.getString(string.Cancel), null);
		d.show();
	}

	@Override
	protected void onUpdateUI() {
		// TODO Auto-generated method stub
		tvWhatScreen.setText(res.getString(string.settingScreenName));
		setContentView(R.layout.settings);
		lvSettings = (ListView) findViewById(R.id.lvSettings);
		lvSettings.setAdapter(settingsAdapter);
		initEventClick();
	}
}
